package cn.gs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.gs.domain.Customer;
import cn.gs.domain.LinkMan;
import cn.gs.domain.Visit;
import cn.gs.utils.HibernateUtil;

@Component(value="VisitDao")
@Scope("prototype")
public class VisitDaoImpl implements VisitDao {
	
	
	
	Session session = HibernateUtil.openSession();
	Transaction transaction = session.getTransaction();
	@Override
	public List<Customer> selectCustom() {
		transaction.begin();
		Query query =session.createQuery("from Customer");
		List<Customer> list=query.list();
		transaction.commit();
		session.close();
		return list;	
	}
	@Override
	public void add(Visit v) {
		transaction.begin();
		session.save(v);
		transaction.commit();
		session.close();	
	}
	@Override
	public List<Visit> paging(int page) {
		transaction.begin();		
		Query query = session.createQuery("from Visit");
		int i=(page-1)*3;
		query.setFirstResult(i);
		query.setMaxResults(3);
		List<Visit> list = query.list();		
		transaction.commit();
		session.close();
		return list;
	}
	@Override
	public List<Visit> selectPageCount() {
		transaction.begin();
		Query query = session.createQuery("select count(v) from Visit v");
		List list = query.list();
		transaction.commit();
		session.close();
		return list;
	}
	@Override
	public Visit selectByID(String id) {
		transaction.begin();	
		Visit v = (Visit) session.get(Visit.class, id);
		transaction.commit();
		session.close();
		return v;
	}
	@Override
	public void update(Visit v) {
		transaction.begin();
		System.out.println(v.toString());
		session.saveOrUpdate(v);		
		transaction.commit();
		session.close();
		
	}
	@Override
	public void delete(String id) {
		transaction.begin();
		Visit v = (Visit) session.get(Visit.class, id);
		session.delete(v);
		transaction.commit();
		session.close();
		
	}
	@Override
	public List<Visit> selName(String name) {
		transaction.begin();
		Query query=session.createQuery("from Visit where visit_interviewee like ?");
		query.setParameter(0, "%"+name+"%");
		List list=query.list();	
		transaction.commit();
		session.close();
		return list;	
	}

}
